/**
 * Main package for application source files
 */
package TextEditor;

import static org.junit.Assert.*;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import TextEditor.facade.AdditiveCopyOpertion;
import TextEditor.facade.AdditiveCutOperation;
import TextEditor.facade.CopyOperation;
import TextEditor.facade.FileInterface;

import static org.mockito.Mockito.*;

/**
 * @author Timur Utepov
 *
 */
public class TestEditorFunctions {
	EditorWindow editorMock;
	EditorWindow editorActual;
	JFileChooser fileChooserMock;
	JTextArea textAreaMock;
	FileInterface fileInterface;
	Toolkit toolkit;
	Clipboard clipBoard;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		editorMock = mock(EditorWindow.class);
		editorActual = new EditorWindow();
		fileChooserMock = mock(JFileChooser.class);
		textAreaMock =  mock(JTextArea.class);
		toolkit = Toolkit.getDefaultToolkit();
		clipBoard = toolkit.getSystemClipboard();
		when (textAreaMock.getSelectedText()).thenReturn(", added text");
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testIsNewFile() {
		when (editorMock.isNewFile()).thenReturn(true);
		
		Boolean actual = editorMock.isNewFile();
		Boolean expected = true;
		assertEquals(expected, actual);
	}
	
	@Test
	public void testSetNewFile() {

		editorActual.setNewFile(true);
		Boolean actual = editorActual.isNewFile();
		Boolean expected = true;
		assertEquals(expected, actual);
	}

	@Test
	public void testSetCurrentFile() {

		editorActual.setCurrentFile(new File("default.txt"));
		File actual = editorActual.getCurrentFile();
		File expected = new File("default.txt");
		assertEquals(expected, actual);
	}
	
	@Test
	public void testSetModified() {

		editorActual.setModified(true);
		Boolean actual = editorActual.isModified();
		Boolean expected = true;
		assertEquals(expected, actual);
	}
	
	@Test
	public void testCreateMenu(){
		String[] items = new String[] { "test1", "test2", "","test3" };
		JMenu testMenu = editorActual.createMenu("TestMenuGroup", items);
		String actual = testMenu.getText();
		String expected = "TestMenuGroup";
		assertEquals(expected, actual);
		
		actual = testMenu.getItem(0).getText();
		expected = "test1";
		assertEquals(expected, actual);
		
		actual = testMenu.getItem(1).getText();
		expected = "test2";
		assertEquals(expected, actual);
		
		actual = testMenu.getItem(3).getText();
		expected = "test3";
		assertEquals(expected, actual);
	}
	
	@Test
	public void testCopyOperation() throws UnsupportedFlavorException, Exception{

		fileInterface = new CopyOperation();
		when (textAreaMock.getSelectedText()).thenReturn("copied text");
		fileInterface.fileOperation(textAreaMock);
		Transferable contents = clipBoard.getContents(this);
		String actual = (String)contents.getTransferData(DataFlavor.stringFlavor);
		String expected = "copied text";
		assertEquals(expected, actual);
	}
	
	@Test
	public void testAdditiveCopyOperation() throws UnsupportedFlavorException, Exception{

		fileInterface = new AdditiveCopyOpertion();
		fileInterface.fileOperation(textAreaMock);
		Transferable contents = clipBoard.getContents(this);
		String actual = (String)contents.getTransferData(DataFlavor.stringFlavor);
		String expected = "copied text, added text";
		assertEquals(expected, actual);
	}
	
	
}
